package com.lab.web.controller.biz;

import com.lab.common.annotation.Log;
import com.lab.common.core.controller.BaseController;
import com.lab.common.core.domain.AjaxResult;
import com.lab.common.core.page.TableDataInfo;
import com.lab.common.enums.BusinessType;
import com.lab.common.enums.LabStatus;
import com.lab.common.utils.poi.ExcelUtil;
import com.lab.system.domain.Equipment;
import com.lab.system.domain.EquipmentBorrow;
import com.lab.system.service.IEquipmentBorrowService;
import com.lab.system.service.IEquipmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;

/**
 * 实验设备借用Controller
 *
 * @author cf
 * @date 2025-02-15
 */
@RestController
@RequestMapping("/system/borrow")
public class EquipmentBorrowController extends BaseController {
    @Autowired
    private IEquipmentBorrowService equipmentBorrowService;

    @Autowired
    private IEquipmentService equipmentService;

    /**
     * 查询实验设备借用列表
     */
    @GetMapping("/list")
    public TableDataInfo list(EquipmentBorrow equipmentBorrow) {
        startPage();
        List<EquipmentBorrow> list = equipmentBorrowService.selectEquipmentBorrowList(equipmentBorrow);
        return getDataTable(list);
    }


    /**
     * 查询实验设备借用列表
     */
    @GetMapping("/list/me")
    public TableDataInfo listMe(EquipmentBorrow equipmentBorrow) {
        startPage();
        equipmentBorrow.setUserId(getUserId());
        List<EquipmentBorrow> list = equipmentBorrowService.selectEquipmentBorrowList(equipmentBorrow);
        return getDataTable(list);
    }


    /**
     * 导出实验设备借用列表
     */
    @Log(title = "实验设备借用", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, EquipmentBorrow equipmentBorrow) {
        List<EquipmentBorrow> list = equipmentBorrowService.selectEquipmentBorrowList(equipmentBorrow);
        ExcelUtil<EquipmentBorrow> util = new ExcelUtil<EquipmentBorrow>(EquipmentBorrow.class);
        util.exportExcel(response, list, "实验设备借用数据");
    }

    /**
     * 获取实验设备借用详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(equipmentBorrowService.selectEquipmentBorrowById(id));
    }

    /**
     * 新增实验设备借用
     */
    @Log(title = "实验设备借用", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody EquipmentBorrow equipmentBorrow) {
        // 获取当前用户
        if (equipmentBorrow.getUserId() == null) {
            equipmentBorrow.setUserId(getUserId());
        }
        equipmentBorrow.setCreatedBy(getUsername());
        equipmentBorrow.setUpdatedBy(getUsername());
        equipmentBorrow.setCreatedAt(new Date());
        equipmentBorrow.setUpdatedAt(new Date());
        return toAjax(equipmentBorrowService.insertEquipmentBorrow(equipmentBorrow));
    }

    /**
     * 修改实验设备借用
     */
    @Log(title = "实验设备借用", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody EquipmentBorrow equipmentBorrow) {
        return toAjax(equipmentBorrowService.updateEquipmentBorrow(equipmentBorrow));
    }

    /**
     * 删除实验设备借用
     */
    @Log(title = "实验设备借用", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(equipmentBorrowService.deleteEquipmentBorrowByIds(ids));
    }

    /**
     * 实验设备归还
     */
    @Log(title = "实验设备借用", businessType = BusinessType.UPDATE)
    @PutMapping("/return/{id}")
    public AjaxResult returnEquipment(@PathVariable("id") Long id) {
        EquipmentBorrow equipmentBorrow = equipmentBorrowService.selectEquipmentBorrowById(id);
        if (equipmentBorrow == null) {
            return error("借用记录不存在");
        }
        if (equipmentBorrow.getStatus() != 1) {
            return error("当前状态不支持归还");
        }
        equipmentBorrow.setUpdatedBy(getUsername());
        equipmentBorrow.setUpdatedAt(new Date());
        equipmentBorrow.setStatus(2L);

        Equipment equipment = equipmentService.selectEquipmentById(equipmentBorrow.getEquipmentId());
        if (equipment != null) {
            equipment.setStatus(LabStatus.OK.getCode());
            equipmentService.updateEquipment(equipment);
        }

        return toAjax(equipmentBorrowService.updateEquipmentBorrow(equipmentBorrow));
    }
}
